<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;

class SummaryDate extends Base
{

    public function getOptions(): array
    {
        return [
            'pk'    => 'id',
            'model' => \app\common\model\SummaryDate::class
        ];
    }


    public function index(): string
    {
        $table = Table::init($this);
        $table->addColumn('create_date', '日期', fn(Col $col) => $col->width(120)->totalRowText('合计'));
        foreach (getBonusTitle(2) as $k => $v) {
            $table->addColumn($k, $v, fn(Col $col) => $col->totalRow(true));
        }
        return $table
            ->setCheckbox(false)
            ->addColumn('update_time', '更新时间')
            ->addRightButtons([
                'edit' => fn(Button $b) => $b->title('查看明细')->url(url('summary_info')->build())
            ])
            ->addSearch([
                ['create_date', '日期筛选', fn(Search $s) => $s->type('date')->dateType('date')],
            ])
            ->setTotalRow(true)
            ->enableExport()
            ->fetch();

    }

    public function summary_info($id)
    {
        $info = $this->model::find($id);
        $s = $info->create_date . ' - ' . date("Y-m-d", strtotime("+1 day", strtotime($info->create_date)));
        $url = url('account/bonus') . '?create_time=' . $s;
        return redirect($url);

    }

}
